<?php

/**
 * This is the model class for table "{{adse}}".
 *
 * The followings are the available columns in table '{{adse}}':
 * @property integer $zds_id
 * @property integer $zdz_id
 * @property string $zds_title
 * @property string $zds_desc
 * @property double $zds_price
 * @property string $zds_name
 * @property string $zds_mob
 * @property integer $zds_image
 * @property string $zds_file_name
 * @property string $zds_file_type
 * @property string $zds_file_url
 * @property string $zds_url
 * @property string $zds_img_alt
 * @property integer $zds_start_time
 * @property integer $zds_end_time
 * @property integer $zds_priority
 * @property integer $zds_hits
 */
class Adse extends CActiveRecord
{
	//关联分类id，搜索用
	public $catid;
	//关联分类类型，1产品2资讯
	public $cattype;
	//菜单id
	public $znaid='';
	//广告状态，0不限制1未开始2进行中3已下线
	public $status=0;
	public $statusOptions=array(
				0=>'全部',
				1=>'未开始',
				2=>'进行中',
				3=>'已下线',
			);
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Adse the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{adse}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zdz_id, zds_title', 'required'),
			array('zdz_id, zds_image, zds_start_time, zds_end_time, zds_priority, zds_hits', 'numerical', 'integerOnly'=>true),
			array('zds_price', 'numerical'),
			array('zds_title, zds_name', 'length', 'max'=>50),
			array('zds_mob', 'length', 'max'=>11),
			array('zds_file_name', 'length', 'max'=>100),
			array('zds_file_type, zds_file_url, zds_category, zds_url', 'length', 'max'=>255),
			array('zds_img_alt', 'length', 'max'=>25),
			array('zds_desc', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zds_id, zdz_id, zds_title, zds_desc, zds_price, zds_name, zds_mob, zds_image, zds_file_name, zds_file_type, zds_file_url, zds_url, zds_img_alt, zds_start_time, zds_end_time, zds_priority, zds_hits', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'adzone'=>array(self::BELONGS_TO, 'Adzone', 'zdz_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zds_id' => 'Zds',
			'zdz_id' => '广告位',
			'zds_title' => '广告标题',
			'zds_category' => '关联分类',
			'zds_desc' => '广告描述',
			'zds_price' => '实际价格',
			'zds_name' => '客户姓名',
			'zds_mob' => '客户手机',
			'zds_image' => '是否有图片',
			'zds_file_name' => '源文件名称',
			'zds_file_type' => '源文件类型',
			'zds_file_url' => '源文件地址',
			'zds_url' => '链接地址',
			'zds_img_alt' => '图片替换文本',
			'zds_start_time' => '开始时间',
			'zds_end_time' => '结束时间',
			'zds_priority' => '权重',
			'zds_hits' => '点击',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		switch ($this->status){
			case 1:$criteria->compare('zds_start_time','>'.time());
			break;
			case 2:$criteria->compare('zds_start_time','<'.time());
				   $criteria->compare('zds_end_time','>'.time());
			break;
			case 3:$criteria->compare('zds_end_time','<'.time());
			break;
			default :;
			break;
		}
		if(($this->catid && $this->cattype) || $this->znaid){
			$criteria->join = 'left join {{adzone}} adzone ON t.zdz_id=adzone.zdz_id';
		}
		if($this->znaid){
			$criteria->compare('adzone.zna_id',$this->znaid);
		}
		if($this->catid && $this->cattype){
			$criteria->compare('adzone.zdz_associate',$this->cattype);
			$criteria->addCondition(new CDbExpression('FIND_IN_SET('.$this->catid.',zds_category)'));
		}
		$criteria->compare('zds_id',$this->zds_id);
		$criteria->compare('t.zdz_id',$this->zdz_id);
		$criteria->compare('zds_title',$this->zds_title,true);
		$criteria->compare('zds_desc',$this->zds_desc,true);
		$criteria->compare('zds_price',$this->zds_price);
		$criteria->compare('zds_name',$this->zds_name,true);
		$criteria->compare('zds_mob',$this->zds_mob,true);
		$criteria->compare('zds_image',$this->zds_image);
		$criteria->compare('zds_file_name',$this->zds_file_name,true);
		$criteria->compare('zds_file_type',$this->zds_file_type,true);
		$criteria->compare('zds_file_url',$this->zds_file_url,true);
		$criteria->compare('zds_url',$this->zds_url,true);
		$criteria->compare('zds_img_alt',$this->zds_img_alt,true);
		$criteria->compare('zds_start_time',$this->zds_start_time);
		$criteria->compare('zds_end_time',$this->zds_end_time);
		$criteria->compare('zds_priority',$this->zds_priority);
		$criteria->compare('zds_hits',$this->zds_hits);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * 批量操作
	 * $idbox id数组
	 * $memberid 会员id
	 * */
	public function batchChange($idbox){
		$condition = 'zds_id in('.implode(',', $idbox).')';
		if(Yii::app()->request->getParam('action',0)=='上线'){
			$condition .= ' and zds_end_time<'.time();
			return $this->updateAll(array('zds_end_time'=>time()+3600*24*30),$condition);
		}elseif(Yii::app()->request->getParam('action',0)=='下线'){
			return $this->updateAll(array('zds_end_time'=>time()),$condition);
		}elseif(Yii::app()->request->getParam('action',0)=='批量删除'){
			return $this->deleteAll($condition);
		}
	}
	
	/**
	 * 返回关联分类的广告数
	 * */
	public function categoryAds($type,$cid){
		$sql = 'SELECT COUNT(a.zds_id) amount FROM {{adse}} a LEFT JOIN {{adzone}} z ON a.zdz_id=z.zdz_id WHERE z.zdz_associate='.$type.' AND FIND_IN_SET('.$cid.',a.zds_category)';
		$command = Yii::app()->db->createCommand($sql)->queryColumn();
		print_r($command[0]);
		
	}
}